package com.ican.oc.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ican.oc.dao.OcCategoryDao;
import com.ican.oc.dao.OcCategoryDescriptionDao;
import com.ican.oc.dao.OcDataSynDao;
import com.ican.oc.dao.OcProductDao;
import com.ican.oc.dao.OcProductDescriptionDao;
import com.ican.oc.dao.OcProductImageDao;
import com.ican.oc.dao.OcProductOptionDao;
import com.ican.oc.dao.OcProductOptionValueDao;
import com.ican.oc.dao.OcProductToCategoryDao;
import com.ican.oc.domain.OcCategory;
import com.ican.oc.domain.OcCategoryDescription;
import com.ican.oc.domain.OcProduct;
import com.ican.oc.domain.OcProductDescription;
import com.ican.oc.domain.OcProductImage;
import com.ican.oc.domain.OcProductOption;
import com.ican.oc.domain.OcProductOptionValue;
import com.ican.oc.domain.OcProductToCategory;

@Service("ocProductService")
public class OcProductService{

    private OcCategoryDao ocCategoryDao;
    private OcCategoryDescriptionDao ocCategoryDescriptionDao;
    private OcProductDao ocProductDao;
    private OcProductDescriptionDao ocProductDescriptionDao;
    private OcProductImageDao ocProductImageDao;
    private OcProductOptionDao ocProductOptionDao;
    private OcProductOptionValueDao ocProductOptionValueDao;
    private OcProductToCategoryDao ocProductToCategoryDao;
    
    private OcDataSynDao ocDataSynDao;

    //商品类目
    public void addOcCategory(OcCategory ocCategory){
        ocCategoryDao.save(ocCategory);
    }
    public OcCategory findOcCategory(Integer categoryId){
        return ocCategoryDao.get(categoryId);
    }
    public void addOcCategoryDescription(OcCategoryDescription ocCateDesc){
        ocCategoryDescriptionDao.save(ocCateDesc);
    }
    public OcCategoryDescription findOcCateDescByCateIdAndLang(Integer categoryId, Integer languageId){
        return ocCategoryDescriptionDao.findOcCateDescByCateIdAndLang(categoryId, languageId);
    }
    public void updateOcCateDesc(Integer categoryId, Integer languageId, String name, String description){
        ocCategoryDescriptionDao.updateOcCateDesc(categoryId, languageId, name, description);
    }
    public void addOcCategoryAndDesc(OcCategory ocCategory, OcCategoryDescription ocCateDesc_ch, OcCategoryDescription ocCateDesc){
        ocCategoryDao.save(ocCategory);
        if(ocCateDesc_ch != null){
            ocCateDesc_ch.getPk().setCategoryId(ocCategory.getCategoryId());
            ocCategoryDescriptionDao.save(ocCateDesc_ch);
        }
        if(ocCateDesc != null){
            ocCateDesc.getPk().setCategoryId(ocCategory.getCategoryId());
            ocCategoryDescriptionDao.save(ocCateDesc);
        }
    }
    
    //价格单位与重量单位
    public Double getPriceRate(String unit){
        Double d = ocDataSynDao.getPriceRate(unit);
        if(d == null){
            return ocDataSynDao.getPriceRate("$");
        }
        return d;
    }
    public Integer getWeightUnitId(String unit){
        return ocDataSynDao.getWeightUnitId(unit);
    }
    
    //商品
    public void addOcProduct(OcProduct ocProduct){
        ocProductDao.save(ocProduct);
    }
    public void addOcProductDescription (OcProductDescription ocProDesc){
        ocProductDescriptionDao.save(ocProDesc);
    }
    public void addOcProductAndDesc(OcProduct ocProduct, OcProductDescription ocProDesc_ch, OcProductDescription ocProDesc){
        ocProductDao.save(ocProduct);
        if(ocProDesc_ch != null){
            ocProDesc_ch.getPk().setProductId(ocProduct.getProductId());
            ocProductDescriptionDao.save(ocProDesc_ch);
        }
        if(ocProDesc != null){
            ocProDesc.getPk().setProductId(ocProduct.getProductId());
            ocProductDescriptionDao.save(ocProDesc);
        }
    }
    public OcProduct findOcProduct (Integer id){
        return ocProductDao.get(id);
    }
    public OcProductDescription findOcProductDesc(Integer productId, Integer languageId) {
        return ocProductDescriptionDao.findOcProductDesc(productId, languageId);
    }
    public void updateOcProductDesc (Integer productId, Integer languageId, String name, String description){
        ocProductDescriptionDao.updateOcProductDesc(productId, languageId, name, description);
    }
    public void updateOcProductMainImage(Integer productId, String image){
        ocProductDao.updateOcProductMainImage(productId, image);
    }
    public OcProductImage findOcProductImage(Integer productId, String image) {
        return ocProductImageDao.findOcProductImage(productId, image);
    }
    public void addOcProductImage(OcProductImage ocProductImage){
        ocProductImageDao.save(ocProductImage);
    }
    public void updateOcProductImage(Integer productImageId, String image){
        ocProductImageDao.updateOcProductImage(productImageId, image);
    }
    public OcProductOption findOcProductOption(Integer productId, Integer optionId){
         return ocProductOptionDao.findOcProductOption(productId, optionId);
    }
    public void addOcProductOption(OcProductOption ocProductOption){
        ocProductOptionDao.save(ocProductOption);
    }
    public OcProductOptionValue findOcProductOptionValue(Integer productOptionId, Integer optionValueId){
        return ocProductOptionValueDao.findOcProductOptionValue(productOptionId, optionValueId);
    }
    public void addOcProductOptionValue(OcProductOptionValue ocProductOptionValue){
        ocProductOptionValueDao.save(ocProductOptionValue);
    }
    
    //商品与类目关系
    public OcProductToCategory findOcProductToCategory(Integer productId, Integer categoryId){
        return ocProductToCategoryDao.findUnique("from OcProductToCategory where pk.productId = " + 
                productId + " and pk.categoryId = " + categoryId);
    }
    public void addOcProductToCategory(OcProductToCategory ocProductToCategory){
        ocProductToCategoryDao.save(ocProductToCategory);
    }
    
    @Resource
    public void setOcDataSynDao(OcDataSynDao ocDataSynDao) {
        this.ocDataSynDao = ocDataSynDao;
    }
    @Resource
    public void setOcCategoryDao(OcCategoryDao ocCategoryDao) {
        this.ocCategoryDao = ocCategoryDao;
    }
    @Resource
    public void setOcCategoryDescriptionDao(OcCategoryDescriptionDao ocCategoryDescriptionDao) {
        this.ocCategoryDescriptionDao = ocCategoryDescriptionDao;
    }
    @Resource
    public void setOcProductDao(OcProductDao ocProductDao) {
        this.ocProductDao = ocProductDao;
    }
    @Resource
    public void setOcProductDescriptionDao(OcProductDescriptionDao ocProductDescriptionDao) {
        this.ocProductDescriptionDao = ocProductDescriptionDao;
    }
    @Resource
    public void setOcProductImageDao(OcProductImageDao ocProductImageDao) {
        this.ocProductImageDao = ocProductImageDao;
    }
    @Resource
    public void setOcProductOptionDao(OcProductOptionDao ocProductOptionDao) {
        this.ocProductOptionDao = ocProductOptionDao;
    }
    @Resource
    public void setOcProductOptionValueDao(OcProductOptionValueDao ocProductOptionValueDao) {
        this.ocProductOptionValueDao = ocProductOptionValueDao;
    }
    @Resource
    public void setOcProductToCategoryDao(OcProductToCategoryDao ocProductToCategoryDao) {
        this.ocProductToCategoryDao = ocProductToCategoryDao;
    }
}
